package com.cvicse.jrcompetition01.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.cvicse.jrcompetition01.common.lang.Result;
import com.cvicse.jrcompetition01.entity.Department;
import com.cvicse.jrcompetition01.entity.Mechanism;
import com.cvicse.jrcompetition01.entity.MechanismContact;
import com.cvicse.jrcompetition01.entity.MechanismDepartment;
import com.cvicse.jrcompetition01.mapper.MechanismDepartmentMapper;
import com.cvicse.jrcompetition01.mapper.MechanismMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.cvicse.jrcompetition01.controller.BaseController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者:kissshotwang@163.com
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/mechanism-department")
public class MechanismDepartmentController extends BaseController {

    @Resource
    MechanismMapper mechanismMapper;

    @Resource
    MechanismDepartmentMapper mechanismDepartmentMapper;

    /**
     * 动态获取下拉框
     *
     * @param
     * @return
     */
    @GetMapping("/selectList")
    public Result selectList(@RequestParam("id") Integer id) {
        //根据机构名称查找机构 （机构名称具有设置唯一性）
        MechanismContact mechanismContact = mechanismContactService.getById(id);
        String mechanismName = mechanismContact.getMechanismName();
        Mechanism selectOne = mechanismMapper.selectOne(new QueryWrapper<Mechanism>().eq("mechanism_name", mechanismName));
        //拿到该机构拥有的所有部门id
        List<Integer> allIds = mechanismDepartmentMapper.getAll(selectOne.getId());
        List<Department> departments = departmentService.listByIds(allIds);
        List<String> nameList = new ArrayList<>();
        for (Department d : departments) {
            nameList.add(d.getMechanismDepartment());
        }
        String[] toBeStored = nameList.toArray(new String[nameList.size()]);
        return Result.succ(toBeStored);
    }

}
